﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Customers
{
    public partial class EditGroup : System.Web.UI.Page
    {
        #region Properties

        private CustomerGroupBiz _customerGroupBiz;

        private CustomerGroupBiz CustomerGroupManager
        {
            get { return _customerGroupBiz ?? (_customerGroupBiz = new CustomerGroupBiz()); }
        }

        private int _groupId;
        private int GroupId
        {
            get
            {
                if (_groupId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _groupId);
                return _groupId;
            }
        }

        private CustomerGroup _customerGroup;
        private CustomerGroup CurrentCustomerGroup
        {
            get
            {
                if (_customerGroup == null && GroupId > 0)
                {
                    _customerGroup = CustomerGroupManager.GetById(GroupId);
                }
                return _customerGroup;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                if (CurrentCustomerGroup == null)
                {
                    lblErrorMessage.Text = lblErrorMessage.Text = string.Format("{0}: Id={1}", Resources.GlobalStrings.NotExisted, GroupId);
                    btnSave.Enabled = false;
                }
                else
                {
                    txtCode.Text = CurrentCustomerGroup.Code;
                    txtName.Text = CurrentCustomerGroup.Name;
                    txtDescription.Text = CurrentCustomerGroup.Description;
                    txtSortOrder.Text = CurrentCustomerGroup.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentCustomerGroup.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;

            CurrentCustomerGroup.Code = code;
            CurrentCustomerGroup.Name = name;
            CurrentCustomerGroup.Description = descrition;
            if (sortOrder > 0)
                CurrentCustomerGroup.SortOrder = sortOrder;
            CurrentCustomerGroup.Hidden = isHidden;

            // update
            CustomerGroupManager.UpdateGroup(CurrentCustomerGroup);

            Response.Redirect("Groups.aspx");
        }

        #endregion
    }
}